import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class MT1599成绩排名 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        // code here
        int n = input.nextInt();
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            students.add(new Student(input.next(), input.nextInt()));
        }
        students.sort((s1, s2) -> {
            if (s1.score != s2.score) {
                return s2.score - s1.score;// 降序：大的减小的
            } else {
                // String.compareTo() 是升序比较
                // s2.compareTo(s1) 就实现了降序比较
                return s2.name.compareTo(s1.name);
            }
        });
        for (Student student : students) {
            System.out.println(student.name);
        }
        input.close();
    }
}
class Student{
    public String name;
    public int score;
    public Student(String name, int score){
        this.name = name;
        this.score = score;
    }
}